/**
 * 
 * Copyright (C) 2011 Cody Stoutenburg . All rights reserved.
 *
 *       This program is free software; you can redistribute it and/or
 *       modify it under the terms of the GNU Lesser General Public License
 *       as published by the Free Software Foundation; either version 2.1
 *       of the License, or (at your option) any later version.
 *
 *       This program is distributed in the hope that it will be useful,
 *       but WITHOUT ANY WARRANTY; without even the implied warranty of
 *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *       GNU Lesser General Public License for more details.
 *
 *       You should have received a copy of the GNU Lesser General Public License
 *       along with this program; if not, write to the Free Software
 *       Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 * 
 */
package ca.usherbrooke.mainApp.view.utils;

import java.util.ArrayList;
import java.util.List;

import javax.swing.table.AbstractTableModel;

/**
 * @author Cody Stoutenburg
 * 
 */
public class MyTableModel extends AbstractTableModel {
	private static final long serialVersionUID = 1L;

	private final List<Class<?>> allClass;
	private final List<String> allTitles;
	private final List<List<Object>> allValues;

	public MyTableModel() {
		allClass = new ArrayList<Class<?>>();
		allTitles = new ArrayList<String>();
		allValues = new ArrayList<List<Object>>();
	}

	@Override
	public String getColumnName(int column) {
		return allTitles.get(column);
	}

	@Override
	public int getRowCount() {
		return allValues.size() + 1;
	}

	@Override
	public int getColumnCount() {
		return allTitles.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		Object value = allTitles.get(columnIndex);
		if (rowIndex > 0) {
			try {
				value = allValues.get(rowIndex - 1).get(columnIndex);
			} catch (IndexOutOfBoundsException ex) {
				throw new IndexOutOfBoundsException("Row index: " + rowIndex
						+ ", col index: " + columnIndex + ", row count: "
						+ allValues.size() + ", col count: "
						+ allValues.get(0).size());
			}
		}
		return value;
	}

	public void addColumn(Class<?> columnClass, String title) {
		allClass.add(columnClass);
		allTitles.add(title);

		for (List<Object> values : allValues) {
			values.add(null);
		}
		this.fireTableStructureChanged();
	}

	@Override
	public Class<?> getColumnClass(int columnIndex) {
		return Object.class;
	}

	public void addRow(List<Object> values) {
		allValues.add(values);
		this.fireTableStructureChanged();
	}

	public void removeRow(int row) {
		allValues.remove(row);
		this.fireTableStructureChanged();
	}

	public void clear() {
		allValues.clear();
		this.fireTableStructureChanged();
	}
}
